<?php
/**
 * Class Coupon  卡券快照操作
 */
namespace Pri\snapshot;
use Model\CouponSnapshotMod;

class CouponSnapshot extends Snapshot
{
    private $CouponSnapshot = null;

    public function __construct($_data)
    {
        $this->_data = $_data;
        $this->trimData();
    }

    /**
     * 整理数据
     */
    private function trimData()
    {
        $_data = array(
            'name'=>$this->_data['name'],
            'rule'=>$this->_data['rule']
        );
        if(isset($this->_data['cost_type'])) $_data['cost_type'] = $this->_data['cost_type'];
        if(isset($this->_data['money'])) $_data['money'] = $this->_data['money'];
        if(isset($this->_data['icon'])) $_data['icon'] = $this->_data['icon'];
        if(isset($this->_data['cost'])) $_data['cost'] = $this->_data['cost'];
        if(isset($this->_data['cover'])) $_data['cover'] = $this->_data['cover'];
        if(isset($this->_data['images'])) $_data['images'] = $this->_data['images'];
        unset($this->_data['name'],$this->_data['cost_type'],$this->_data['rule'],$this->_data['cost'],$this->_data['money'],$this->_data['icon'],$this->_data['cover'],$this->_data['images']);
        $this->_data['detail'] = json_encode($_data);
    }

    /**
     * 保存快照
     * @param $_from store = 门店卡券  sponsor = 活动后台投放卡券
     */
    public function save($_from = 'store')
    {
        $this->CouponSnapshot = new CouponSnapshotMod();
        $_where = array();
        if($_from == 'store'){
            $_where = array('activity_id'=>$this->_data['activity_id'],'store_id'=>$this->_data['store_id'],'uid'=>$this->_data['uid'],'coupon_id'=>$this->_data['coupon_id'],'activity_coupon_id'=>$this->_data['activity_coupon_id'],'past_time'=>$this->_data['past_time'],'edit_time'=>$this->_data['edit_time']);
        }else{
            $_where = array('activity_id'=>$this->_data['activity_id'],'uid'=>$this->_data['uid'],'coupon_id'=>$this->_data['coupon_id'],'past_time'=>$this->_data['past_time'],'edit_time'=>$this->_data['edit_time']);
        }
        $_one_snapshot = $this->CouponSnapshot->findOne($_where,array('id'));
        if($_one_snapshot){
            $this->addNumber($this->CouponSnapshot,$_one_snapshot['id']);
        }else{
            $this->add($this->CouponSnapshot);
        }
    }


}